package com.zxhysy.log.annotation.filter;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

import java.util.Set;

/**
 * json 序列化过滤属性工具
 *
 * @author zxh
 * @since 2023/5/27 17:08:55
 */
public class JsonFilterUtil {

    /**
     * 添加过滤的字段，这里过滤的是
     */
    public static void addCustomExceptFilterForMapper(ObjectMapper mapper, Set<Class<?>> ignore) {
        SimpleBeanPropertyFilter filter = new CustomExceptFilter();
        FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", filter);
        for (Class<?> aClass : ignore) {
            mapper.addMixIn(aClass, FieldFilterMixIn.class);
        }
        mapper.setFilterProvider(filters);
    }

    /**
     * 定义一个类或接口
     */
    @JsonFilter("myFilter")
    public interface FieldFilterMixIn{
    }

}
